package com.gsit.job.flows;

import java.util.Date;
import java.util.List;

import org.openqa.selenium.By.ById;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gsit.job.config.Config;
import com.gsit.job.domain.Person;
import com.gsit.job.domain.ResumeCaptureState;
import com.gsit.job.services.PersonFileWriter;
import com.gsit.job.services.PersonService;

@Component
public class ResumeWriteFlow extends FlowServiceAbs {

	
	@Autowired
	private PersonFileWriter fileWriter;
	
	@Autowired
	private PersonService personService;
	
	private void createResume(Person person) {
		StringBuilder st = new StringBuilder();
		st.append(Config.RESUME_URL);
		st.append("resid="+person.getResid());
		st.append("&uid="+person.getUid());
		////System.out.println("Link "+st.toString());
		driver.get(st.toString());
		//Date startDate = new Date();
		if(isValidResumePage(driver.getPageSource())){
			
			if(isResumeAvailable(driver.getPageSource())){
				//Date firstDate = new Date();
				fileWriter.write(person, driver.getPageSource());
				////System.out.println("File write processing time "+(new Date().getTime() - firstDate.getTime()));
				try {
					person.setName(findName());
					person.setEmail(findEmail());
				} catch (Exception e) {
				}
				
				person.setResumeCaptureState(ResumeCaptureState.SUCCESS);
			}else{
				person.setResumeCaptureState(ResumeCaptureState.NOT_AVAILABLE);
			}
			//Date saveDate = new Date();
			updatePerson(person);
			//System.out.println("Data base write processing time "+(new Date().getTime() - saveDate.getTime()));
		}else{
			callWaitForCaptcha();
			createResume(person);
		}
		
		//System.out.println("Final processing time "+(new Date().getTime() - startDate.getTime()));
		
	}
	
	private String findName()throws Exception{
		//Date startDate = new Date();
		WebElement element = driver.findElement(ById.className("heading_5"));
		String name =  element.getText();
		//System.out.println("findName processing time "+(new Date().getTime() - startDate.getTime()));
		return name;
	}
	private String findEmail()throws Exception{
		//Date startDate = new Date();
		String email = "";
		List<WebElement> links=driver.findElements(By.xpath("//a[starts-with(@href, 'mailto:')]"));
		for (WebElement element : links) {
			if(!element.getText().trim().equals("")){
				email = element.getText();
				break;
			}
			
		}
		//System.out.println("findEmail processing time "+(new Date().getTime() - startDate.getTime()));
		return email;
	}
	
	private String findPhoneNumber(){
		return "";
	}
	
	private Boolean isResumeAvailable(String content){
		boolean result = true;
		//Date startDate = new Date();
		if(content.contains("Resume is no longer available")){
			result = false;
		}
		//System.out.println("isResumeAvailable processing time "+(new Date().getTime() - startDate.getTime()));
		return result;
	}
	
	private Boolean isValidResumePage(String content) {
		Boolean result =  Boolean.TRUE;
		//Date startDate = new Date();
		WebElement element =null;
		try {
			element = driver.findElement(ById.id("jAlert"));
		} catch (Exception e) {
			
		}
		if(element !=null && element.isDisplayed()){
			//System.out.println("jAlert.isDisplayed :"+element.isDisplayed());
			result =  Boolean.FALSE;
		}else if (content.contains("Please authenticate yourself")) {
			//System.out.println("Captcha is found....");
			result =  Boolean.FALSE;
		}
		//System.out.println("isValidResumePage processing time "+(new Date().getTime() - startDate.getTime()));
		return result;
	}
	
	private void updatePerson(Person person ){	
		person.setIsResumeCaptured(true);
		person.setResumeCaptureDate(new Date());
		personService.saveOrUpdate(person);
	}

	@Override
	protected void process() throws Exception {
		List<Person> persons = personService.findByResumeCaptured(false);
		
		for (Person person : persons) {
			createResume(person);
		}
		
	}

	@Override
	protected void processTest() {
		// TODO Auto-generated method stub
		
	}

}
